#include <sys/types.h>
#include<sys/socket.h>
#include<pthread.h>
#include<netinet/in.h>
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<stdlib.h>

int main(int argc,char **argv){
	if(argc!=3){
		printf("please%d  port\n",argc);
		exit(1);
	}
	printf("%s  %s\n",argv[1],argv[2]);

	struct sockaddr_in addr;
	addr.sin_family=AF_INET;
	addr.sin_port=htons(atoi(argv[2]));
	addr.sin_addr.s_addr=htonl(INADDR_ANY);

	int sock;
	sock=socket(AF_INET,SOCK_DGRAM,0);
	if(sock<0){
		perror("socket:");
		exit(1);
	}
	if(bind(sock,(struct sockaddr *)&addr,sizeof(addr))<0){
		printf("%d===\n",sock);
		perror("bind:");
		exit(1);
	}
	char buff[512];
	struct sockaddr_in clientAddr;
	int n;
	int len=sizeof(clientAddr);
	while(1){
		n=recvfrom(sock,buff,512,0,(struct sockaddr *)&clientAddr,&len);
		if(n>0){
			buff[n]=0;
			printf("%s",buff);
			strcpy(buff,"nihao\n");
			n=sendto(sock,buff,512,0,(struct sockaddr *)&clientAddr,sizeof(clientAddr));
			if(n<0){
				perror("sento");
				break;
			}
		}else{
			printf("err:\n");
			perror("recv");
		}
	}
	return 0;
}
